﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Negocios;

public partial class MaterAlbum : System.Web.UI.Page
{
    AlbumBLL aBLL = new AlbumBLL();
    

    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        txtNomeAlbum.Enabled = true;
        ddlAno.Enabled = true;
        txtUsuario.Enabled = true;
        btnGrava.Enabled = true;
        btnCancelar.Enabled = true;
        btnNovo.Enabled = false;
    }

    private void limpar()
    {
        txtCod.Text = "";
        txtNomeAlbum.Text = "";
        txtUsuario.Text = "";
        ddlAno.SelectedValue = "0";
        lblerro.Text = "";

        txtNomeAlbum.Enabled = false;
        ddlAno.Enabled = false;
        txtUsuario.Enabled = false;
        btnGrava.Enabled = false;
        btnCancelar.Enabled = false;
    }

                                                                                                              

    protected void btnCancelar_Click(object sender, EventArgs e)
    {
        limpar();
        btnNovo.Enabled = true;
    }

    protected void btnGrava_Click(object sender, EventArgs e)
    {
        try
        {
            if (txtCod.Text == "")
            {
                Album a = new Album(txtNomeAlbum.Text, Convert.ToInt32(ddlAno.SelectedValue), txtUsuario.Text);
                aBLL.Insere(a);
            }
            else
            {
                Album a = new Album(Convert.ToInt32(txtCod.Text), txtNomeAlbum.Text, Convert.ToInt32(ddlAno.SelectedValue), txtUsuario.Text);
                aBLL.Atualizar(a);
            }
            limpar();
            btnNovo.Enabled = true;
            GridView1.DataBind();
        }
        catch (Exception ex)
        {
            lblerro.Text = ex.Message;
        }
    }

    protected void lbRemove_Click(object sender, EventArgs e)
    {
        try
        {
            int id = Convert.ToInt32(((GridViewRow)((DataControlFieldCell)((LinkButton)sender).Parent).Parent).Cells[0].Text);
            Album a = new Album(id);
            aBLL.Excluir(a);
            GridView1.DataBind();
        }
        catch (Exception ex)
        {
            lblerro.Text = ex.Message;
        }
    }
    protected void lbAtualiza_Click(object sender, EventArgs e)
    {
        try
        {
            int id = Convert.ToInt32(((GridViewRow)((DataControlFieldCell)((LinkButton)sender).Parent).Parent).Cells[0].Text);
            Album a = aBLL.Consultar(id);
            txtCod.Text = a.Cod.ToString();
            txtNomeAlbum.Text = a.Nome;
            ddlAno.SelectedValue = a.CodAno.ToString();
            txtUsuario.Text = a.Usuario;
            GridView1.DataBind();

            txtNomeAlbum.Enabled = true;
            ddlAno.Enabled = true;
            txtUsuario.Enabled = true;
            btnGrava.Enabled = true;
            btnCancelar.Enabled = true;
            btnNovo.Enabled = false;
        }
        catch (Exception ex)
        {
            lblerro.Text = ex.Message;
        }

        

        
    }
}
